package board2;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import connection.Reader;

public class Board2 extends JPanel implements MouseListener {

	int height = 80;
	int width = 80;

	boolean red = false;

	boolean[][] isPressed = new boolean[8][8];
	String[][] positions = new String[8][8];

	int[][] memory = new int[2][2];
	BufferedWriter bw;

	String from = "";
	int which = 0;

	int wh;

	char[] convertor = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
	int[] reverse= { 8, 7, 6, 5, 4, 3, 2, 1 };
	int[] reverse2= { 7, 6, 5, 4, 3, 2, 1,0 };

	JPanel board;
	Color white = new Color(255, 255, 255);
	Color black = new Color(0);

	private Dimension size = new Dimension(height * 8 + 15, width * 8 + 35);

	int redX;
	int redY;

	Hashtable<String, Image> pieces = new Hashtable<String, Image>();
	public Hashtable<String, Integer> convertLetToNum = new Hashtable<String, Integer>();

	public void init() {
		bw = Reader.getWriter();
		board = new JPanel();
		this.add(board);
		board.addMouseListener(this);
		board.setPreferredSize(size);
		
		convertLetToNum.put("a", 0);
		convertLetToNum.put("b", 1);
		convertLetToNum.put("c", 2);
		convertLetToNum.put("d", 3);
		convertLetToNum.put("e", 4);
		convertLetToNum.put("f", 5);
		convertLetToNum.put("g", 6);
		convertLetToNum.put("h", 7);

		pieces.put("P", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/wp.gif"));
		pieces.put("p", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/bp.gif"));
		pieces.put("K", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/wk.gif"));
		pieces.put("k", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/bk.gif"));
		pieces.put("Q", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/wq.gif"));
		pieces.put("q", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/bq.gif"));
		pieces.put("B", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/wb.gif"));
		pieces.put("b", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/bb.gif"));
		pieces.put("N", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/wn.gif"));
		pieces.put("n", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/bn.gif"));
		pieces.put("R", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/wr.gif"));
		pieces.put("r", Toolkit.getDefaultToolkit().getImage(
				"board/pieces/br.gif"));
	}

	public void paint(Graphics g) {

		int a = 0;

		Color[] colors = { white, black };

		for (int y = 0; y < height * 8; y += height) {
			for (int x = 0; x < width * 8; x += width) {
				g.setColor(colors[a % 2]);
				if (isPressed[x / width][y / height] == true) {
					g.setColor(Color.RED);
				}
				g.fillRect(x, y, height, width);
				a++;
			}
			a++;
		}
		int desetina = width / 10;

		for (int y = 0; y < height * 8; y += height) {
			for (int x = 0; x < width * 8; x += width) {
				g.setColor(colors[a % 2]);
				g.fillRect(x + desetina, y + desetina, height - (2 * desetina),
						width - (2 * desetina));
				a++;
			}
			a++;
		}

		for (int y = 0; y < height * 8; y += height) {
			for (int x = 0; x < width * 8; x += width) {
				if (positions[x / width][y / height] != null) {
					g.drawImage(pieces.get(positions[x / width][y / height]),
							x, y, height, width, null);
				}
			}

		}
	}

	public Dimension getSize() {
		return size;
	}

	public void setPosition(int posX, int posY, String piece) {
		positions[posX][posY] = piece;
		repaint();
	}

	public void switchPressed(int x, int y) {
		isPressed[x / width][reverse2[y / height]*height] = !isPressed[x / width][y / height];
		repaint();
	}

	public void setPressed(int x, int y) {
		isPressed[x][reverse2[y]] = true;
		System.out.println("press"+x+","+y);
		repaint();
	}

	public void unsetPressed(int x, int y) {
		isPressed[x][reverse2[y]] = false;
		repaint();
	}

	public boolean getPressed(int x, int y) {
		return isPressed[x / width][y / height];
	}

	public void mouseClicked(MouseEvent arg0) {

		// System.out.println((arg0.getX() + width) / width + ","
		// + (arg0.getY() + height) / height);

		switchPressed(arg0.getX(), arg0.getY());
		red = getPressed(arg0.getX(), arg0.getY());
		memory[which][0] = arg0.getX();
		memory[which][1] = arg0.getY();

		from = from.concat(String
				.valueOf(convertor[((arg0.getX() + width) / width) - 1]));
		from = from.concat(String
				.valueOf(reverse[((arg0.getY() + height) / height) - 1]));

		if (red) {
			which++;
		} else {
			which--;
		}
		if (which > 1) {
			try {
				bw.write(from + "\n");
				bw.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println(from);
			from = "";
			which = 0;
			switchPressed(memory[0][0], memory[0][1]);
			switchPressed(memory[1][0], memory[1][1]);
		}
	}

	// @Override
	public void mouseEntered(MouseEvent arg0) {

	}

	// @Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	// @Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	// @Override
	public void mouseReleased(MouseEvent arg0) {

	}

}
